package com.example.myapplication.tack_14;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.tack_14.adapter.ClassificaAdapter;
import com.example.myapplication.tack_14.data.BannerData;
import com.example.myapplication.tack_14.data.Classification02Data;
import com.example.myapplication.tack_14.data.ClassificationData;
import com.example.myapplication.utils.BannerTool;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class ClassificationActivity extends AppCompatActivity {
    private ImageView exit;
    private ViewPager banner;
    private RecyclerView fenlei;
    private TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_14_activity_classification);
        init();
        exit_();
        getBanner();
        getFenLei();
        getCilak();
    }

    private void getCilak() {
        String url = "prod-api/api/garbage-classification/garbage-classification/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, Classification02Data.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                Classification02Data classificationData = (Classification02Data)obj;
                if(classificationData.getCode() == 200){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            for (Classification02Data.RowsDTO row : classificationData.getRows()) {
                                TabLayout.Tab tab= tabLayout.newTab();
                                tab.setText(row.getName());
                                tabLayout.addTab(tab);
                            }
                            Sort(classificationData);
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void Sort(Classification02Data classificationData) {
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                int sort = classificationData.getRows().get(0).getSort();
                Intent intent = new Intent(ClassificationActivity.this,NewsFenLeiPageActivity.class);
                intent.putExtra(Common.COMMON,String.valueOf(sort));
                ClassificationActivity.this.startActivity(intent);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }




    private void getFenLei() {
        String url = "prod-api/api/garbage-classification/garbage-example/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, ClassificationData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                ClassificationData classificationData = (ClassificationData)obj;
                if(classificationData.getCode() == 200){
                    List<ClassificationData.RowsDTO> classList = classificationData.getRows();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            FenleiAdater(classList);
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void FenleiAdater(List<ClassificationData.RowsDTO> classList) {
        ClassificaAdapter classificaAdapter = new ClassificaAdapter(classList, ClassificationActivity.this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(ClassificationActivity.this,4);
        fenlei.setLayoutManager(gridLayoutManager);
        fenlei.setAdapter(classificaAdapter);
    }
    private void getBanner() {
        String url = "prod-api/api/garbage-classification/ad-banner/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, BannerData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                BannerData bannerData = (BannerData)obj;
                if(bannerData.getCode() == 200){

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ArrayList<String> arrayList = new ArrayList<>();
                            for (BannerData.DataDTO row : bannerData.getData()) {
                                arrayList.add(row.getImgUrl());
                            }
                            BannerTool bannerTool = new BannerTool(ClassificationActivity.this,arrayList,banner);
                            banner.setAdapter(bannerTool);
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }
    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClassificationActivity.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        banner = findViewById(R.id.banner);
        fenlei = findViewById(R.id.fenlei);
        tabLayout = findViewById(R.id.tabLayout);


    }
}